import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


public class TablePanel extends JPanel{
	private int defaultNumStations = 8;
	private StationDataModel sdm; 
	private JTextPane numStations;
	JTable mainTable;
	private double diameterinput = 0;
	private JTextPane myDiameter;
	private JTextPane myCirc;

	public TablePanel( final DataManager newData ){
		
		mainTable = new JTable();
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		//unless a lable is going to be changed it doesn't need a name
		//applications never set fonts it is up to the os to do that
		
		add( new JLabel("Settlement Values"));
		
		JPanel diameterPanel = new JPanel();
		diameterPanel.setLayout( new BoxLayout( diameterPanel, BoxLayout.LINE_AXIS));
		diameterPanel.add( new JLabel("Diameter:"));
		diameterPanel.add( Box.createRigidArea(new Dimension(10, 0)));
		
		myDiameter = new JTextPane();
		diameterPanel.add(myDiameter);
		myDiameter.setText( Double.toString(diameterinput) );
		diameterPanel.add( Box.createRigidArea(new Dimension(10, 0)));
		diameterPanel.add( new JLabel("Circumference:"));
		diameterPanel.add( Box.createRigidArea(new Dimension(10, 0)));
		myCirc = new JTextPane();
		myCirc.setText( Double.toString(diameterinput*Math.PI) );
		diameterPanel.add(myCirc);
		add(diameterPanel);
		
		add( Box.createRigidArea(new Dimension(0, 10)));
		
		JPanel stationsPanel = new JPanel();
		stationsPanel.setLayout( new BoxLayout( stationsPanel, BoxLayout.LINE_AXIS));
		stationsPanel.add( new JLabel("Locations:"));
		stationsPanel.add( Box.createRigidArea(new Dimension(6, 0)));
		
		numStations = new JTextPane();
		numStations.setText( Integer.toString(defaultNumStations) );
		stationsPanel.add(numStations);
		add(stationsPanel);

		add( Box.createRigidArea(new Dimension(0, 10)));
		
		JPanel buttonPanel = new JPanel();
		JButton stationUpdate = new JButton("Update Table");
		stationUpdate.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent e){
				if (Integer.parseInt(numStations.getText())<8){
					numStations.setText("8");
				}else if (Integer.parseInt(numStations.getText()) % 2 != 0){
					numStations.setText(Integer.toString(Integer.parseInt(numStations.getText())-1));
				}
				if (myCirc.getText().equals("")){
					myCirc.setText( Double.toString(Double.parseDouble(myDiameter.getText())*Math.PI) );
				}else if (Double.parseDouble(myCirc.getText()) == 0){
					myCirc.setText( Double.toString(Double.parseDouble(myDiameter.getText())*Math.PI) );
				}else{
					myDiameter.setText( Double.toString(Double.parseDouble(myCirc.getText())/Math.PI) );
				}
				sdm = new StationDataModel( Integer.parseInt( numStations.getText() ),Double.parseDouble(myDiameter.getText()), newData);
				mainTable.setModel( sdm );
			}
		});
		buttonPanel.add(stationUpdate);
		buttonPanel.add( Box.createRigidArea( new Dimension(10, 0)));
		JButton calcSett = new JButton("Calculate");
		calcSett.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				
				
			}
			
		});
		buttonPanel.add(calcSett);
		buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.LINE_AXIS));
		add( buttonPanel ); 

		add( Box.createRigidArea(new Dimension(0, 10)));
		
		sdm = new StationDataModel( defaultNumStations,  diameterinput, newData);
		mainTable.setModel(sdm);
		JScrollPane scrollpane = new JScrollPane(mainTable);
		add(scrollpane);
    
	}
}

class StationDataModel extends AbstractTableModel {
	public static final NumberFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat ("0.00");
    private String[] columnNames = {"Location" , "Elevation" , "Arc Location" };
    private Object[][] data;

    public StationDataModel(int numStations, double myD, DataManager myDatam){
    	data  = new Object[ numStations ][3];
    	createEmptyTable(numStations, myD, myDatam);
    	for( int i = 0; i < data.length; i++){
    		isCellEditable(i,0);
    		isCellEditable(i,1);
    		isCellEditable(i,2);
    	}
    }
    
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        
            return true;
        
    }
    public void setValueAt(Object value, int row, int col) {
    	
    	//we need to verify data here
    	//notice value is an Object
    	//this allows for generic code
    	//
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

	private void createEmptyTable(double myLocs, double myD, DataManager myDatam){

		String s = DEFAULT_DECIMAL_FORMAT.format(0.0);
		BigDecimal newSpot = new BigDecimal (s);
		BigDecimal myMore = new BigDecimal (myD*Math.PI/data.length);
    	myDatam.clear();
    	for( int i = 0; i < data.length; i++){
    		data[i][0] = (double)i+1.0;
    		data[i][1] = 1.0;
    		data[i][2] = DEFAULT_DECIMAL_FORMAT.format(newSpot);
    		newSpot = newSpot.add (myMore);
    		if (myDatam.size() != myLocs){
    			myDatam.addLast(data);
    		}
    	}
    }
}
